package com.tenglong.teacher.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.tenglong.teacher.R;
import com.tenglong.teacher.data.resultInfo.homework.HomeWork;
import com.tenglong.teacher.ui.adapter.viewholder.base.BaseViewHolder;
import com.tenglong.teacher.ui.adapter.viewholder.homework.StudentCommitViewHolder;
import com.tenglong.teacher.ui.adapter.viewholder.homework.StudentPublishViewHolder;

import java.util.List;


/**
 * Created by wim on 4/14/16.
 */
public class HomeworkStudentListAdapter extends RecyclerView.Adapter<BaseViewHolder<HomeWork>> {

    private HomeworkStudentListListener listListener;
    private List<HomeWork> singleList;

    public HomeworkStudentListAdapter(List<HomeWork> data) {
        singleList = data;
    }

    public void remove(HomeWork item) {
        int position = singleList.indexOf(item);
        if (position > -1) {
            singleList.remove(position);
            notifyItemRemoved(position);
        }
    }

    public void clear() {
        while (getItemCount() > 0) {
            remove(getItem(0));
        }
    }

    public HomeWork getItem(int position) {
        return singleList.get(position);
    }


    @Override
    public BaseViewHolder<HomeWork> onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        switch (viewType) {
            case 0:
                return new StudentPublishViewHolder(inflater.inflate(R.layout.item_homework_stu_header, parent, false), listListener);
            default:
                return new StudentCommitViewHolder(inflater.inflate(R.layout.item_homework_stu_list, parent, false), listListener);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return singleList.get(position).isHeader ? 0 : 1;
    }

    @Override
    public void onBindViewHolder(final BaseViewHolder<HomeWork> holder, int position) {
        holder.setLayoutInfo(singleList.get(position));
    }

    @Override
    public int getItemCount() {
        return singleList.size();
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
    }

    public void setHomeworkStudentListListener(HomeworkStudentListListener listListener) {
        this.listListener = listListener;
    }

    public interface HomeworkStudentListListener {
        void onRecordPosition(int position);

        int getNotSubmitStudentNumber();
    }

}

